<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <fileTrees @handleNodeClick="onTreeClick" :tableHeight="treeHeight" :isnoClick="isnoClick" :menuId="menuId"></fileTrees>
      </el-col>
      <!--离线接收数据-->
      <el-col :span="20" :xs="24" v-show="isShow">
        <queryCriteriaList v-if="listFrom.length > 0" :showSearch="showSearch" :listFrom="listFrom"
                           @resetQuery="resetQuery" @handleQuery="handleQuery" ref="queryForm"></queryCriteriaList>
        <el-row :gutter="10" class="mb8" ref="refbut">
<!--          <el-col :span="1.5" v-if="(subStr == 'X' && categoryType == '3')||(subStr == 'V' && categoryType == '1')||(subStr == 'F' && categoryType == '0')">-->
<!--            <el-button type="primary" plain icon="el-icon-takeaway-box"-->
<!--                       @click="handleJieshouTap()">接收</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="el-icon-edit"-->
<!--                       @click="handledeitTap(1)">修改</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" plain icon="el-icon-delete"-->
<!--                       @click="handleDeleteTap()">删除</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5" v-show="(subStr == 'F' && categoryType == '3') || (subStr == 'F' && categoryType == '1') || (subStr == 'F' && categoryType == '0')">
            <el-button type="primary" plain icon="el-icon-takeaway-box"
                       @click="handleQuXiaoJieshouTap()"
                       v-hasPermi="['archive:wjzl:cancelAccept']"
            >退回至在线接收模块</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryTableTap"></right-toolbar>
        </el-row>
        <tableList v-if="tableHeight" :tableHeaderList="tableHeaderList"
                   @handleSelectionChange="handleSelectionChange" :tableData="tableData" :loading="loading"
                   :tableHeight="tableHeight" @celldblclick="celldblclick" @sortChange="sortChange" ref="ref"></tableList>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getlistPost" />
      </el-col>
      <el-col :span="20" :xs="24" v-show="!isShow" class="empty">
        <el-empty :image-size="200"></el-empty>
      </el-col>

    </el-row>
    <!-- start 组件著录、修改 -->
    <jiandeit v-if="jianOpen" :open="jianOpen" :openInfo="openInfo" @closeDelDialog="closeDelDialog"
              @submitDetail="submitDetail" :isParams="1">
    </jiandeit>
    <!-- end  组件著录、修改-->

    <!-- start 组卷著录、修改 -->
    <juandeit v-if="juanOpen" :open="juanOpen" :openInfo="openInfo" @closeDelDialog="closeDelDialog"
              @submitDetail="submitDetail" :isParams="1"></juandeit>
    <!-- end  组卷著录、修改-->

    <!-- start 项目著录  修改 -->
    <projectdeit v-if="projectOpen" :open="projectOpen" :openInfo="openInfo" @submitDetail="submitDetail"
                 @closeDelDialog="closeDelDialog" :isParams="1"></projectdeit>
    <!-- end 项目著录、修改 -->

    <!--start  组件详情 -->
    <jianDetailsOpen v-if="jianDetailsOpen" :open="jianDetailsOpen" :celldbData="celldbData"
                     @detailsHandleClose="detailsHandleClose">
    </jianDetailsOpen>
    <!--end 组件详情 -->
    <!-- start 组卷\项目详情 -->
    <juanDetailsOpen v-if="juanDetailsOpen" :open="juanDetailsOpen" :celldbData="celldbData"
                     @detailsHandleClose="detailsHandleClose"></juanDetailsOpen>
    <!-- end 组卷\项目详情 -->
  </div>
</template>
<script>
// import { getqueryfieldList, getlistbytreenodeList, getthbytreenodeHeaderList, deleteData, jieshou } from "@/api/archive/fileCollection/offlinReception.js";
import { getqueryfieldList, getlistbytreenodeList, getthbytreenodeHeaderList } from "@/api/archive/fileCollection/documentDatabase.js";
import queryCriteriaList from "@/components/queryCriteriaList/index.vue"  //查询列表
import fileTrees from "@/components/fileTrees/index.vue"  //档案树
import { getGroupTree } from "@/api/archive/archivesCategory.js"
import tableList from "@/components/tableList/index.vue"
import jiandeit from "@/views/archive/fileCollection/offlinReception/jiandeit.vue" //件添加修改弹窗
import juandeit from "@/views/archive/fileCollection/offlinReception/juandeit.vue" //卷添加修改弹窗
import projectdeit from "@/views/archive/fileCollection/offlinReception/projectdeit.vue" //卷添加修改弹窗
import treeHeight from '@/mixins/treeHeight'//计算左侧树的高度
import jianDetailsOpen from '@/components/componentsJianDetails/index.vue' //件详情
import juanDetailsOpen from '@/components/componentsJuanDetails/index.vue' //卷详情'
import {getLastIndexOf} from "@/utils/index.js"
import {cancelAccept} from "@/api/archive/fileCollection/fileOrganization";
export default {
  mixins: [treeHeight], // 混入方法
  components: {
    queryCriteriaList,
    fileTrees,
    jiandeit,
    juandeit,
    tableList,
    projectdeit,
    jianDetailsOpen,
    juanDetailsOpen
  },
  data() {
    return {
      menuId: '',
      isnoClick: true, //防止树重复点击
      scroll: "",
      loading: false,
      // 显示搜索条件
      showSearch: true,
      listFrom: [], //查询框列表
      isShow: false, //是否显示右侧数据
      categoryinfo: "", //档案门类回显信息
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableHeaderList: [], //table表头数据
      tableData: [],//table列表数据
      jianOpen: false, //组件打开关闭
      juanOpen: false, //组卷打开关闭
      projectOpen: false,//项目打开关闭
      jianDetailsOpen: false,//件详情弹窗
      juanDetailsOpen: false,//卷详情弹窗
      openInfo: { //件 卷  项目
        opens: false,
        categoryId: undefined,
        menuId: undefined,
        type: undefined
      },
      tableHeight: "",
      ids: undefined,
      single: "",
      multiple: "",
      rowList: "", //当前点击的数据
      subStr: "",  //获取点击当前树的 件 卷  项目 截取 sub_table_name显示
      categoryType: "",
    }
  },

  created() {
    this.menuId = this.$route.meta.menuId
  },


  methods: {
    //动态设置table的高度
    tableHeightData() {
      this.$nextTick(
        () => {
          let h = document.documentElement.clientHeight
          //     console.log(this.$refs.queryForm.$el.offsetHeight, this.$refs.refbut.$el.offsetHeight, "sdklfjklliuhaiorng")
          h = h - 200
          if (this.$refs.queryForm) { // 页面上如果有面包屑区域徐减去它的高度
            h = h - this.$refs.queryForm.$el.offsetHeight
          }

          if (this.$refs.refbut) { // 页面上如果有分页区域徐减去它的高度
            h = h - this.$refs.refbut.$el.offsetHeight
          }
          // console.log(h,"sdjfdklsjfkl")
          this.tableHeight = h > 310 ? h : 310 // 根据自己项目定高度
          return h
        }
      )

    },


    // 树节点单击事件
    onTreeClick(data) {
      // console.log(data,"+++")
      this.isnoClick = false;
      this.treeRowData = data;
      if (data.categoryId != null) {
        this.isShow = true
      } else {
        this.isShow = false
      }
      if (this.$refs.queryForm != undefined) {
        this.$refs.queryForm.queryParams = {};
      }
      if (data.nodeType == null || data.nodeType == "t" || data.nodeType == "f") {
        this.isnoClick = true;
        return false;
      }
      this.loading = true;
      this.queryParams = data;
      this.queryParams.state = 999;
      // this.queryParams.jsType = 1;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.order='';
      this.queryParams.subTableName = data.sub_table_name
      if (this.queryParams.subTableName != undefined) {
        this.subStr = getLastIndexOf(this.queryParams.subTableName);
        let stb = this.queryParams.subTableName;
        let stbArr = stb.split("_");
        this.categoryType = stbArr[stbArr.length - 3];
      } else {
        this.getGroupTreePost(data.categoryId);
      }
      this.getqueryfieldListPost();//点击树节点获取查询字段
      this.getlistHeaderPost();/**点击树节点,获取table表头数据 */
      if(this.$refs.ref!=undefined){
        this.$refs.ref.clearSort()
      }
    },


    //档案门类树门类根据categoryID回显
    getGroupTreePost(ID) {
      getGroupTree(ID).then(response => {
        if (response.data.type == "0") { //件
          this.queryParams.subTableName = response.data.fileTableName;
        } else if (response.data.type == "1") { //卷
          this.queryParams.subTableName = response.data.folderTableName;
        } else { //项目
          this.queryParams.subTableName = response.data.itemTableName;
        }
        this.subStr = getLastIndexOf(this.queryParams.subTableName);
        let stb = this.queryParams.subTableName;
        let stbArr = stb.split("_");
        this.categoryType = stbArr[stbArr.length - 3];
      });
    },

    /** 点击树节点获取查询字段 */
    getqueryfieldListPost(data) {
      this.queryParams.searchcolumn = undefined
      getqueryfieldList({
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        subTableName: this.queryParams.subTableName,

      }).then(response => {
        this.listFrom = response.rows;
      });
    },
    /**点击树节点,获取table表头数据 */
    getlistHeaderPost() {
      getthbytreenodeHeaderList({
          categoryId: this.queryParams.categoryId, menuId: this.queryParams.menuId, subTableName: this.queryParams.subTableName
        }
      ).then(response => {
        this.tableHeaderList = response.rows;
        var fields = []
        for (var i in response.rows) {
          fields.push(response.rows[i].FIELD_NAME)
        }
        this.queryParams.fields = fields.toString()
        this.getlistPost();
      });
    },
    /** 点击树节点,获取table数据*/
    getlistPost() {
      getlistbytreenodeList(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
        this.tableHeightData();
        this.isnoClick=true;
      });
    },

    //是否选择数据
    handleSelectionChange(ids, single, multiple) {
      this.ids = ids.toString();
      this.single = single;
      this.multiple = multiple;
    },

    //表单重置
    resetQuery() {
      this.queryParams.searchcolumn = undefined
      this.queryParams.order = undefined;
      this.$refs.ref.clearSort()
      this.getlistPost();
    },
    //点击搜索
    handleQuery(searchcolumn) {

      var column = searchcolumn.filter(item => { return item.value !== ''&& item.value !== null})
      this.queryParams.searchcolumn = column
      this.queryParams.order = undefined;
      this.$refs.ref.clearSort()
      this.getlistPost();
    },
    //刷新页面
    queryTableTap(){
      this.queryParams.order = undefined;
      this.getlistPost();
      this.$refs.ref.clearSort()
    },

    //件  this.jianOpen  著录和修改     //组卷 this.juanOpen 著录和修改    //项目 this.projectdOpen 著录和修改
    handledeitTap(e) {
      this.openInfo = {
        opens: true,
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        folderId: undefined, //组卷id
        itemId: undefined, //项目Id
        subTableName: this.queryParams.subTableName,
        nodeType: this.queryParams.nodeType,
        parentNodeType: this.queryParams.parentNodeType,
        parentSubTableName: this.queryParams.parentSubTableName,
        subStr: this.subStr,
      }
      if (e == 1) {  //修改
        if (this.ids == undefined) {
          this.$message({ message: '请选择要修改的数据哦', type: 'warning' })
          return false
        }
        if (this.single) {
          this.$message({ message: '请选择一条要修改的数据哦', type: 'warning' })
          return false
        }

        this.openInfo.ids = this.ids; //组件Id;
      } else { //添加
        this.openInfo.ids = undefined
      }
      if (this.subStr == "F") {  //F 件
        this.openInfo.type = 0
        this.jianOpen = true
      } else if (this.subStr == "V") { //卷
        this.openInfo.type = 1
        this.juanOpen = true;
      } else { //项目
        this.openInfo.type = 3
        this.projectOpen = true;
      }
    },
    //给项目案卷著录
    handledeitanJuanTap() {
      this.openInfo = {
        opens: true,
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        type: 1, //传卷类型  0,组件 1，组卷 3，项目
        folderId: undefined, //组卷id
        itemId: undefined, //项目Id
        subTableName: this.queryParams.subTableName
      }
      console.log(this.openInfo)
      if (this.ids == undefined) {
        this.$message({ message: '请选择先选择数据哦', type: 'warning' })
        return false
      }
      if (this.single) {
        this.$message({ message: '请选择一条数据哦', type: 'warning' })
        return false
      }
      this.openInfo.itemId = this.ids; //项目Id;
      this.juanOpen = true;
    },

    //关闭著录 \件\ 组卷
    closeDelDialog() {
      this.jianOpen = false //件
      this.juanOpen = false //卷
      this.projectOpen = false; //项目
      this.openInfo = ""
    },

    //著录成功
    submitDetail() {
      this.jianOpen = false;
      this.juanOpen = false //卷
      this.projectOpen = false; //项目
      this.getlistHeaderPost();
    },

    //删除
    // handleDeleteTap() {
    //   if (this.ids != undefined&&!this.multiple) {
    //     this.$modal.confirm('是否确认删除所选数据项？').then((res) => {
    //       let type = "";
    //       if (this.subStr == "F") {  //F 件
    //         type = 0;
    //       } else if (this.subStr == "V") { //卷
    //         type = 1;
    //       } else { //项目
    //         type = 3;
    //       }
    //       deleteData({
    //         type:type,
    //         //nodeType: this.queryParams.nodeType,
    //         categoryId: this.queryParams.categoryId,
    //         //sub_table_name: this.queryParams.subTableName,
    //         //menuId: this.queryParams.menuId,
    //         //parentNodeType: this.queryParams.parentNodeType,
    //         //parentSubTableName: this.queryParams.parentSubTableName,
    //         ids: this.ids
    //       }).then(res=>{
    //         if(res.code=="200"){
    //           this.$modal.msgSuccess("删除成功");
    //           this.getlistPost();
    //         }
    //       })
    //
    //
    //     }).catch(() => { });
    //   } else {
    //     this.$message({ message: '请选择要删除的数据哦', type: 'warning' })
    //   }
    // },

    //接收
    // handleJieshouTap() {
    //   // console.log(this.ids)
    //   if (this.ids != undefined&&!this.multiple) {
    //     this.$modal.confirm('是否确认接收所选数据数据项？').then(function () {
    //     }).then(() => {
    //       jieshou({
    //         categoryId: this.queryParams.categoryId,
    //         ids: this.ids
    //       }).then(res => {
    //         this.getlistPost();
    //         this.$modal.msgSuccess("接收成功");
    //         setTimeout(res => {
    //           this.$router.push({ path: "/fileCollection/fileOrganization" })
    //         }, 1000)
    //       })
    //
    //     }).catch(() => { });
    //   } else {
    //     this.$message({ message: '请选择要接收的数据哦', type: 'warning' })
    //   }
    // },



    //双击列表进入详情页面
    celldblclick(row) {
      var level = "";
      var label="";
      if (getLastIndexOf(this.queryParams.subTableName) == "F") {  //F 件
        level = 0
        label= row.TITLE;
      } else if (getLastIndexOf(this.queryParams.subTableName) == "V") { //卷
        level = 1
        label= row.TITLE;
      } else { //项目
        level = 3
        label= row.XMMC;
      }
      this.celldbData = {
        id: row.ID,
        categoryId: this.queryParams.categoryId,
        menuId: this.queryParams.menuId,
        sub_table_name: this.queryParams.subTableName,
        label: this.queryParams.label,
        level: level,
        ifSub_table_name: this.queryParams.subTableName,
        isDel: "0",  //判断是回收站数据还是接收收据 isDel:1  回收站    isDel:0 //离线接收、档案接收、在线接收
        tableHeaderList:this.tableHeaderList
      }
      this.$nextTick(() => {
        if (this.subStr == "F") {  //F 进入件详情
          this.jianDetailsOpen = true;
        } else { //进入卷详情 和项目详情
          this.juanDetailsOpen = true
        }
      })
    },
    //关闭详情页面
    detailsHandleClose() {
      this.jianDetailsOpen = false;
      this.juanDetailsOpen = false;
    },

    //排序
    sortChange(val) {
      // console.log(val, "ksdfjkl")
      this.queryParams.order = val;
      // console.log(this.queryParams, "+++")
      this.getlistPost();
    },
    //取消接收
    handleQuXiaoJieshouTap() {
      if (this.ids != undefined && !this.multiple) {
        this.$modal.confirm('是否确认退回所选数据项？').then(function () {
        }).then(() => {
          cancelAccept({
            categoryId: this.queryParams.categoryId,
            ids: this.ids
          }).then(res => {
            this.getlistPost();
            this.$modal.msgSuccess("取消成功");
            // setTimeout(res => {
            //     this.$router.push({ path: "/archive/fileCollection/fileOrganization" })
            // }, 1000)
          })

        }).catch(() => { });
      } else {
        this.$message({ message: '请选择要取消接收的数据哦', type: 'warning' })
      }
    },

  }
}
</script>
